/*
Main.cpp 程序入口文件
feature: 启动服务端
*/

#include <GameServer.h>
#include <json/json.h>
#include <iostream>
#include <fstream>

using namespace std;

/// @brief 服务器启动信息
struct ServerInfo {
    string ip;
    int port = -1;
    int maxPlayer;
    string serverName;
    vector<string> serverInfo;
    float mapWidth = 100.0;
    float mapHeight = 100.0;
    float roundTime = 60.0;
    float itemDropPercentage = 0.6f;
    float youkaiDeployGap = 15.0f;
    int latencyLevel = 1;
    int playerKnockDownScore = 1500;
    int youkaiKnockDownScore = 35;
    float damageScoreRatio = 1.0;
    int youkaiMissPenalize = 1000;
    bool autoStart = false;
    float autoStartTime = 30.0f;
    int autoStartMinPlayerCount = 1;
};

/// @brief 读取server_properties.json获取配置信息
/// @return 服务器配置信息
ServerInfo loadServerProperties() {
    Json::Reader reader;
    Json::Value root;
    ifstream file("server_properties.json", ifstream::binary);
    if (!file.is_open()) { return ServerInfo {}; }
    bool parsingSuccessful = reader.parse(file, root, false);
    if (!parsingSuccessful) { return ServerInfo {}; }
    // 读取文件并反序列化
    ServerInfo result;
    result.ip = root["ip"].asString();
    result.port = root["port"].asInt();
    result.maxPlayer = root["maxPlayer"].asInt();
    result.serverName = root["serverName"].asString();
    result.mapWidth = root["mapWidth"].asFloat();
    result.mapHeight = root["mapHeight"].asFloat();
    result.roundTime = root["roundTime"].asFloat();
    result.itemDropPercentage = root["itemDropPercentage"].asFloat();
    result.youkaiDeployGap = root["youkaiDeployGap"].asFloat();
    result.latencyLevel = root["latencyLevel"].asInt();
    result.playerKnockDownScore = root["playerKnockDownScore"].asInt();
    result.youkaiKnockDownScore = root["youkaiKnockDownScore"].asInt();
    result.damageScoreRatio = root["damageScoreRatio"].asFloat();
    result.youkaiMissPenalize = root["youkaiMissPenalize"].asInt();
    result.autoStart = root["autoStart"].asBool();
    result.autoStartTime = root["autoStartTime"].asFloat();
    result.autoStartMinPlayerCount = root["autoStartMinPlayerCount"].asInt();
    const Json::Value serverInfo = root["serverInfo"];
    for (const auto& info : serverInfo) {
        result.serverInfo.push_back(info.asString());
    }
    // 储存信息到结构体
    return result;
}

int main() {
    GameServer gameServer;
    ServerInfo info = loadServerProperties();
    if (info.port == -1) { printf("Can't read server_properties.json! Check if something goes wrong.\n"); return -1; }
    gameServer.serverName = info.serverName;
    gameServer.serverInfo = info.serverInfo;
    gameServer.mapWidth = info.mapWidth;
    gameServer.mapHeight = info.mapHeight;
    gameServer.roundTime = info.roundTime;
    gameServer.level.roundTime = info.roundTime;
    gameServer.level.youkaiDeployColdDownMax = info.youkaiDeployGap;
    gameServer.level.itemDropPercentage = info.itemDropPercentage;
    gameServer.level.playerKnockDownScore = info.playerKnockDownScore;
    gameServer.level.youkaiKnockDownScore = info.youkaiKnockDownScore;
    gameServer.level.damageScoreRatio = info.damageScoreRatio;
    gameServer.level.youkaiMissPenalize = info.youkaiMissPenalize;
    gameServer.level.autoStart = info.autoStart;
    gameServer.level.autoStartTime = info.autoStartTime;
    gameServer.level.currStartCountDown = info.autoStartTime;
    gameServer.level.autoStartMinPlayerCount = info.autoStartMinPlayerCount;
    gameServer.start(info.ip, info.port, info.maxPlayer);
    // 读取配置信息，并开启服务端
    return 0;
}